package com.example.myui.utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Icon;
import android.icu.text.UFormat;
import android.support.v4.widget.TintableImageSourceView;
import android.text.style.TtsSpan;

import com.lzy.okgo.model.Progress;

import java.util.HashMap;
import java.util.IdentityHashMap;

/**
 * Created by Administrator on 2018/2/2/002.
 */

public class NotifyUtil {

    private Context mContext;
    private NotificationManager manager;
    private Notification.Builder builder;
    private int notifyId;
    private int progress;

    public int getNotifyId() {
        return notifyId;
    }

    public void setNotifyId(int notifyId) {
        this.notifyId = notifyId;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    private HashMap<Integer, Notification> map = new HashMap<>();

    public HashMap<Integer, Notification> getMap() {
        return map;
    }

    public void setMap(HashMap<Integer, Notification> map) {
        this.map = map;
    }

    public NotifyUtil(Context context) {
        this.mContext = context;
        manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        builder = new Notification.Builder(mContext);
    }

    public void creatNotify(int smallIcon,
                            int largeIcon,
                            String title,
                            String text,
                            String ticker,
                            PendingIntent pendingIntent,
                            int id,
                            int progress1) {
        this.notifyId = id;
        this.progress = progress1;
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), largeIcon);
        builder.setSmallIcon(smallIcon)
                .setLargeIcon(bitmap)
                .setContentTitle(title)
                .setContentText(text)
                .setTicker(ticker)
                .setContentIntent(pendingIntent)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true);

    }


    public void updateNotify(int notifyId, int progress) {
        if (progress != 0) {
            builder.setProgress(100, progress, false);
        }

        Notification build = builder.build();
        if (!map.containsKey(notifyId)) {
            map.put(notifyId, build);
        }
        manager.notify(notifyId, build);

    }


    public void cancelNotify(int notifyId) {
        map.remove(notifyId);
        manager.cancel(notifyId);
    }
}
